#include <iostream>
#include <vector>
using namespace std;

bool isSafe(vector<vector<int>>& board, int row, int col, int n) {
    for (int i = 0; i < row; i++) {
        if (board[i][col] == 1) {
            return false;
        }
    }

    for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
        if (board[i][j] == 1) {
            return false;
        }
    }

    for (int i = row, j = col; i >= 0 && j < n; i--, j++) {
        if (board[i][j] == 1) {
            return false;
        }
    }

    return true;
}

void solveNQueensUtil(vector<vector<int>>& board, int row, int n, vector<vector<string>>& result) {
    if (row == n) {
        vector<string> solution;
        for (int i = 0; i < n; i++) {
            string rowStr = "";
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 1) {
                    rowStr += "Q";
                } else {
                    rowStr += ".";
                }
            }
            solution.push_back(rowStr);
        }
        result.push_back(solution);
        return;
    }

    for (int col = 0; col < n; col++) {
        if (isSafe(board, row, col, n)) {
            board[row][col] = 1;
            solveNQueensUtil(board, row + 1, n, result);
            board[row][col] = 0;
        }
    }
}

vector<vector<string>> solveNQueens(int n) {
    vector<vector<int>> board(n, vector<int>(n, 0));
    vector<vector<string>> result;
    solveNQueensUtil(board, 0, n, result);
    return result;
}

int main() {
    int n = 4;  

    vector<vector<string>> result = solveNQueens(n);

    cout << "All possible solutions to the " << n << "-Queens problem are: " << endl;
    for (auto solution : result) {
        for (string row : solution) {
            cout << row << endl;
        }
        cout << endl;
    }

    return 0;
}